/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cx1.service.impl;

import com.rits.cloning.Cloner;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import org.apache.commons.lang.StringUtils;
import org.cx1.dao.api.IAmenityDao;
import org.cx1.dao.impl.AmenityDao;
import org.cx1.model.Amenity;
import org.cx1.model.utils.ServiceResponse;
import org.cx1.service.api.IAmenityService;
import org.cx1.service.utils.IDGenerator;

/**
 *
 * @author ECH
 */
public class DAmenityService implements IAmenityService {

    private IAmenityDao amenityDao = new AmenityDao();
    
    private Cloner cloner = new Cloner();
    
    @Override
    public ServiceResponse<String> create(Amenity amenity) {
        ServiceResponse<String> response = new ServiceResponse<String>();
        String id = null;        
        try {            
            Amenity entry = new Amenity();
            id = IDGenerator.generateID();
            
            entry.setId(id);                        
            entry.setAmenityTypeID(amenity.getAmenityTypeID());
            
            entry.setTitle(amenity.getTitle());
            entry.setDescription(amenity.getDescription());
            entry.setNotes(amenity.getNotes());
                    
            entry.setActive(true);
            entry.setDateCreated(new Date());
            entry.setDateModified(new Date());
                        
            amenityDao.add(amenity);
            
            response.setResponse(id);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response;
    }

    @Override
    public ServiceResponse<Amenity> read(String id) {
        ServiceResponse<Amenity> response = new ServiceResponse<Amenity>();
        Amenity amenity = null;
        try {            
            amenity = amenityDao.getById(id);
            response.setResponse(amenity);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response;
    }

    @Override
    public ServiceResponse<List<Amenity>> readAll(int offset, int limit) {
        ServiceResponse<List<Amenity>> response = new ServiceResponse<List<Amenity>>();
        List<Amenity> amenities = new ArrayList<Amenity>();
        try {            
            List<Amenity> records = amenityDao.getAll();
            if (records != null && records.size() > 0) {
                if (offset == 0 && limit == 0) {
                    amenities.addAll(cloneAmenities(records));
                }
                else if (offset < records.size() && limit < records.size()) {
                    amenities.addAll( cloneAmenities(records.subList(offset, limit)) );
                }              
            }            
            response.setResponse(amenities);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response; 
    }

    @Override
    public ServiceResponse<Boolean> update(Amenity amenity) {
        ServiceResponse<Boolean> response = new ServiceResponse<Boolean>();
        boolean success = false;
        try {            
            String targetID = amenity.getId(); 
            Amenity target = amenityDao.getById(targetID);  
            
            //CX: update dabase record
            if (target != null) {            
                target.setAmenityTypeID(amenity.getAmenityTypeID());

                target.setTitle(amenity.getTitle());
                target.setDescription(amenity.getDescription());
                target.setNotes(amenity.getNotes());

                target.setDateModified(new Date()); 
                
                amenityDao.update(amenity);
                success = true;
            }         
            response.setResponse(success);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response; 
    }

    @Override
    public ServiceResponse<Boolean> delete(String id) {
        ServiceResponse<Boolean> response = new ServiceResponse<Boolean>();
        boolean success = false;
        try {            
            amenityDao.deleteById(id);
            success = true;
            response.setResponse(success);
            response.setSuccessful(true);
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response; 
    }

    @Override
    public ServiceResponse<Amenity> readByTitle(String title) {
        ServiceResponse<Amenity> response = new ServiceResponse<Amenity>();
        Amenity amenity = null;
        try {            
            List<Amenity> records = amenityDao.getAll();
            if (records != null && records.size() > 0) {
                for (Amenity record : records) {
                    if (StringUtils.containsIgnoreCase(record.getTitle(), title)) {
                        //amenity = record;
                        amenity = cloner.deepClone(record);
                        break;
                    }
                }
            }                        
            response.setResponse(amenity);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response;
    }
    
    private List<Amenity> cloneAmenities(List<Amenity> amenities) {
        List<Amenity> cloneList = new ArrayList<Amenity>(0);
        for (Amenity amenity : amenities) {
            cloneList.add(cloneAmenity(amenity));
        }
        return cloneList;
    }
    
    private Amenity cloneAmenity(Amenity amenity) {
        return cloner.deepClone(amenity);
    }
    
    
    public ServiceResponse<String> createImport(Amenity amenity) {
        ServiceResponse<String> response = new ServiceResponse<String>();
        String id = null;        
        try {            
            Amenity entry = new Amenity();
            id = amenity.getId();
            
            entry.setId(id);                        
            entry.setAmenityTypeID(amenity.getAmenityTypeID());
            
            entry.setTitle(amenity.getTitle());
            entry.setDescription(amenity.getDescription());
            entry.setNotes(amenity.getNotes());
                    
            entry.setActive(amenity.isActive());
            entry.setDateCreated(amenity.getDateCreated());
            entry.setDateModified(amenity.getDateModified());
                        
            amenityDao.add(amenity);
            
            response.setResponse(id);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response;
    }
    
}
